package net.trustie.model;

import java.util.List;


import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractBy.Source;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

@ExtractBy("//*[@id='A']/div/table")
public class codeproject_Model implements AfterExtractor, ValidateExtractor {
	private int questionId;

	private String questionUrl;


	private String history = "0";

	private String extractTime;

	private String tag;
	
	@ExtractBy("//*[@id='ctl00_ArticleRating_VotesR']/span[1]/text()")
	private String stars;
	
	@ExtractBy(value = "//*[@id='ctl00_ArticleTitle']/text()")
	private String questionTitle = "";

	@ExtractBy("//*[@id='contentdiv']/allText()")
	private String questionContent = "";

//	@ExtractBy(value = "//*[@id='A']/div/div[1]/div[1]/div/a[3]/span/text()", source = Source.RawHtml)
//	private String categories;
	
//	@Formatter("yyyy-MM-dd HH:mm")
//	@ExtractBy("//*[@id='RHSticky']/div[2]/table/tbody/tr[1]/td[2]/span/text()")
	@ExtractBy("//*[@id='aspnetForm']/div[3]/div[2]/div[1]/div/span[2]/@content")
	private String createTime = "";

	@ExtractBy("//*[@id='ctl00_Nav']/div[1]/div/div[4]/a/allText()")
	private String revision_num = "";
	
//	@ExtractBy("//*[@id='ctl00_TagsList_VisibleTags']/div[@class='T']/allText()")
//	private List<String> tags;
	@ExtractBy("//*div[@class='tags']/span/a/text()")
	private List<String> tags;

	@ExtractBy(value = "//*[@id='ctl00_ArticleTabs_CmtCnt']/text()")
	private String answer_num = "0";

	@ExtractBy("//*[@id='aspnetForm']/div[@class='header']/div[2]/div[1]/div/span[2]/allText()")
	private String lastEditorTime = "";
	
	@ExtractBy("//*[@id='aspnetForm']/div[@class='header']/div[2]/div[1]/div/span[1]/a/span/span/text()")
	private String author="";
	
	@ExtractBy("//*[@id='aspnetForm']/div[@class='header']/div[2]/div[1]/div/span[@class='author']/a/@href")
	private String authorUrl="";

	@ExtractBy("//*[@id='RHSticky']/div[@class='article-summary']/table/tbody/tr[2]/td[2]/text()")
	private String view_num;
	
	@ExtractBy("//*[@id='RHSticky']/div[2]/table/tbody/tr[3]/td[2]/text()")
	private String download_num;

	@ExtractBy("//*[@id='RHSticky']/div[2]/table/tbody/tr[4]/td[2]/text()")
	private String favorite_num;

	private String source = "code project";
	private String type =  "blog";
	
	private String urlMD5;
	
	@ExtractBy("//*[@id='ctl00_ArticleRating_VotesR']/span[@class='rating']/text()")
	private String rate_num;
	@ExtractBy("//*[@id='ctl00_ArticleRating_VotesR']/span[@class='count']/text()")
	private String rate_value;
	
	@ExtractBy(value="//*table[@class='also-read']/tbody/tr/td/div[2]/div/a/@href",source=Source.RawHtml)
	private List<String> related_posts;
	private String related_post;
	
	public void afterProcess(Page page) {
		related_post =  StringHandler.combineTags(related_posts);
		// 处理questionUrl
		this.questionUrl = page.getPageUrl();
		// 处理tag
		this.tag = StringHandler.combineTags(tags);

		// 处理questio nId
		this.questionId = Integer.parseInt(StringHandler.matchRightString(this.questionUrl,"\\d+"));

		// 处理answerNum
		if (StringUtils.isNotBlank(this.answer_num)) {
			this.answer_num = StringHandler.matchRightString(this.answer_num,
					"\\d+");
		} else
			this.answer_num = "0";

		// 处理revision_num
				if (StringUtils.isNotBlank(this.revision_num)) {
					this.revision_num = StringHandler.matchRightString(this.revision_num,
							"\\d+");
				} else
					this.revision_num = "0";
		
		//处理view_num
//		if (StringUtils.isNotBlank(this.view_num))
//		this.view_num=dropComma(this.view_num);
		if(this.view_num != null){
			this.view_num = this.view_num.split(" ")[0];
			try{
				this.view_num = String.valueOf(Float.valueOf(this.view_num));
			}catch(Exception e){
				String purenum = this.view_num.substring(0, this.view_num.length()-1);
				if(this.view_num.endsWith("K")){
					this.view_num = String.valueOf(Float.valueOf(purenum) * 1000);
				}else if(this.view_num.endsWith("M")){
					this.view_num  = String.valueOf(Float.valueOf(purenum) * 1000000);
				}
			}
		}else{
			this.view_num  = "0";
		}
		
				
		// 处理download_num
			if (StringUtils.isNotBlank(this.download_num)) {
					dropComma(this.download_num);
					this.download_num = StringHandler.matchRightString(this.download_num,
								"\\d+");
			} else
				{
				this.download_num = "0";		
				}
				
				
		// 处理favorite_num
			if (StringUtils.isNotBlank(this.favorite_num)) {
					dropComma(this.favorite_num);
					this.favorite_num = StringHandler.matchRightString(this.favorite_num,
							"\\d+");
				}
			else{
				this.favorite_num = this.download_num;	
				this.download_num="0";
				}
	
		// 处理抽取时间
		this.extractTime = DateHandler.getExtractTime();

		
		this.urlMD5 = DigestUtils.md5Hex(this.questionUrl);
		// 处理createTime			
//	    this.createTime=DateHandler.handleTime(DateHandler.dateAddTime(this.createTime));	
//		System.out.println(this.createTime);
//		//this.createTime=DateHandler.handleTime(this.createTime);
//		System.out.println(this.createTime);
	    // 处理lastEditorTime			
	    this.lastEditorTime=DateHandler.handleTime(DateHandler.dateAddTime(this.lastEditorTime)); 
	    
	    //处理authorUrl
	    this.authorUrl="http://www.codeproject.com"+this.authorUrl;
	}
	
	@Override
	public void validate(Page page) {
		// TODO Auto-generated method stub
		
	}
	public String dropComma(String num) {
		if(StringUtils.isBlank(num))
			return "0";
		if(num.length()<=3)
			return num;
		String tmp[]=num.split(",");
		num="";
		for(int i=0;i<tmp.length;i++){
			num+=tmp[i];
		}
		return num;
	}
	public int getQuestionId() {
		return questionId;
	}
	public void setQuestionId(int questionId) {
		this.questionId = questionId;
	}
	
	public String getQuestionUrl() {
		return questionUrl;
	}
	
	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUrlMD5() {
		return urlMD5;
	}

	public void setUrlMD5(String urlMD5) {
		this.urlMD5 = urlMD5;
	}

	public String getRate_num() {
		return rate_num;
	}

	public void setRate_num(String rate_num) {
		this.rate_num = rate_num;
	}

	public String getRate_value() {
		return rate_value;
	}

	public void setRate_value(String rate_value) {
		this.rate_value = rate_value;
	}

	public List<String> getRelated_posts() {
		return related_posts;
	}

	public void setRelated_posts(List<String> related_posts) {
		this.related_posts = related_posts;
	}

	public String getRelated_post() {
		return related_post;
	}

	public void setRelated_post(String related_post) {
		this.related_post = related_post;
	}

	public void setQuestionUrl(String questionUrl) {
		this.questionUrl = questionUrl;
	}

	
	
	public String getHistory() {
		return history;
	}
	public void  setHistory(String history) {
		this.history=history;
	}
	
	
	public String getExtractTime() {
		return extractTime;
	}
	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}
	
	
	public String getTag() {
		return tag;
	}
	public void setTag(String tag) {
		this.tag = tag;
	}
	
	public String getStars() {
		return stars;
	}
	public void setStars(String stars) {
		this.stars = stars;
	}
	
	
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	
	
	public List<String> getTags() {
		return tags;
	}
	public void setTags(List<String> tags) {
		this.tags = tags;
	}
	
	public String getAnswer_num() {
		return answer_num;
	}
	public void setAnswer_num(String answer_num) {
		this.answer_num = answer_num;
	}
	
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getAuthorUrl() {
		return authorUrl;
	}
	public void setAuthorUrl(String authorUrl) {
		this.authorUrl = authorUrl;
	}
	
	public String getFavorite_num() {
		return favorite_num;
	}
	public void setFavorite_num(String favorite_num) {
		this.favorite_num = favorite_num;
	}
	
	public String getDownload_num() {
		return download_num;
	}
	public void setDownload_num(String download_num) {
		this.download_num = download_num;
	}
	
	public String getQuestionTitle() {
		return questionTitle;
	}
	public void setQuestionTitle(String questionTitle) {
		this.questionTitle = questionTitle;
	}
	
	public String getQuestionContent() {
		return questionContent;
	}
	public void setQuestionContent(String questionContent) {
		this.questionContent = questionContent;
	}
	
	
	public String getLastEditorTime() {
		return lastEditorTime;
	}
	public void setLastEditorTime(String lastEditorTime) {
		this.lastEditorTime=lastEditorTime;
	}
	
	
	public String getRevision_num() {
		return revision_num;
	}
	public void setRevision_num(String revision_num) {
		this.revision_num = revision_num;
	}
	public String getView_num() {
		return view_num;
	}

	public void setView_num(String view_num) {
		this.view_num = view_num;
	}
}
